/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.UserEmailManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.MailToIsNullException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.EmailUtil;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.mail.EmailException;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class SendMail extends AbstractAction
/*     */ {
/*  29 */   private static Logger logger = Logger.getLogger(SendMail.class.getName());
/*     */ 
/*     */   public SendMail(AbstractHttpServlet servlet) {
/*  32 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  42 */     User user = super.getLoginUser(request);
/*  43 */     String mailAddress = request.getParameter("mailAddress");
/*  44 */     String subject = request.getParameter("subject");
/*  45 */     String mailcontent = request.getParameter("mailcontent");
/*  46 */     String attachFiles = request.getParameter("attachFiles");
/*  47 */     String baseFilePath = this.servlet.getServletContext().getRealPath(
/*  48 */       "uploadfiles/");
/*     */ 
/*  50 */     response.setContentType("text/html;charset=utf-8");
/*  51 */     PrintWriter out = response.getWriter();
/*  52 */     JSONObject jsonObject = new JSONObject();
/*     */ 
/*  54 */     UserEmailManager manager = new UserEmailManager();
/*     */ 
/*  56 */     Map result = null;
/*     */     try {
/*  58 */       result = manager.getUserEmailSetting(user.userId);
/*     */     } catch (DBException e) {
/*  60 */       logger.warn("get user email setting failed", e);
/*     */     } catch (SQLException e) {
/*  62 */       logger.warn("get user email setting failed", e);
/*     */     }
/*     */ 
/*  65 */     if (result == null) {
/*  66 */       jsonObject.put("error", "1");
/*  67 */       jsonObject.put("errorMsg", "没有设置可以发送的SMTP信息");
/*  68 */       out.println(jsonObject);
/*  69 */       out.flush();
/*  70 */       out.close();
/*  71 */       return;
/*     */     }
/*     */ 
/*  74 */     String host = (String)result.get("Host");
/*  75 */     int port = 25;
/*  76 */     Integer tmp = (Integer)result.get("Port");
/*  77 */     if (tmp != null) {
/*  78 */       port = tmp.intValue();
/*     */     }
/*  80 */     String username = (String)result.get("UserName");
/*  81 */     String password = (String)result.get("UserPwd");
/*  82 */     tmp = (Integer)result.get("IsSSL");
/*  83 */     boolean isSSL = false;
/*  84 */     if ((tmp != null) && (tmp.intValue() == 1)) {
/*  85 */       isSSL = true;
/*     */     }
/*  87 */     String from = (String)result.get("EmailAddress");
/*     */ 
/*  89 */     String sign = (String)result.get("EmailSign");
/*  90 */     if (mailcontent == null) {
/*  91 */       mailcontent = "";
/*     */     }
/*     */ 
/*  94 */     if ((sign != null) && (sign.trim().length() > 0))
/*  95 */       mailcontent = mailcontent + "<br/>" + 
/*  96 */         sign.replaceAll("(\r\n|\n)", "<br/>");
/*     */     try
/*     */     {
/*  99 */       EmailUtil.sendEmail(host, port, username, password, isSSL, from, 
/* 100 */         mailAddress, subject, mailcontent, attachFiles, 
/* 101 */         baseFilePath, user.name);
/*     */ 
/* 103 */       jsonObject.put("error", "0");
/*     */     } catch (EmailException e) {
/* 105 */       jsonObject.put("error", "1");
/* 106 */       jsonObject.put("errorMsg", e.getMessage());
/* 107 */       logger.warn("check email setting failed", e);
/*     */     } catch (MailToIsNullException e) {
/* 109 */       jsonObject.put("error", "1");
/* 110 */       jsonObject.put("errorMsg", "请设置需要发送的邮箱地址");
/* 111 */       logger.warn("MailToIsNullException", e);
/*     */     }
/* 113 */     out.println(jsonObject);
/* 114 */     out.flush();
/* 115 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.SendMail
 * JD-Core Version:    0.6.1
 */